/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * \file
 * \brief Header of Directory reader class
 * \author Antti Maula <antti.maula@tkk.fi>
 *
 */
#ifndef _LAUNCHER_DIRECTORY_HPP_
#define _LAUNCHER_DIRECTORY_HPP_
#include "launchertypes.hpp"
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

/** Utility class for listing filesystem directory contents.
 *
 */
class CDirectory
{
public:

public:
  /** Default constructor.
   */
  CDirectory();

  /** Destructor.
   */
  ~CDirectory();

  /** Open a directory for reading.
   *
   * This functions opens a directory for listing files in it.
   * 
   * @param[in] aDirectory      Path to directory to open.
   * @return                    'true' when directory was succesfully opened,
   *                            'false' when directory opening failed miserably.
   */
  bool Open(const std::string &aDirectory);


  /** Get files in the directory.
   *
   * This function returns regular files in the currently open
   * directory.  The following checks are made: The file must be
   * readable, and it must be regular file (not a directory). Also, if
   * the regular expression is given (size is > 0), all _filenames_
   * (not full paths) are matched agains the given expression and only
   * matching entries are returned.
   *
   * @param[out] aArray         Store array of filenames here.
   * @param[in] aRegExp         Select files based on given regular expression.
   *                            (Extended RE)
   * @return                    'true' when array was succesfully stored 
   *                             with zero or more elements,
   *                            'false' when array generation failed.
   */
  bool GetFileList(TStringArray &aArray, const std::string &aRegExp = "");
 

  /** Get list of regular files and directories under the directory.
   *
   * This function returns regular files and directory entries in the
   * currently open directory.  If the regular expression is given
   * (size is > 0), all _filenames_ (not full paths) are matched
   * agains the given expression and only matching entries are
   * returned.
   *
   * @param[out] aRegularFileArray Store array of regular file filenames here.
   * @param[out] aDirectoryFileArray Store array of directory entries here.
   * @param[in] aRegExp         Select files based on given regular expression.
   *                            (Extended RE)
   * @return                    'true' when arrays were succesfully stored 
   *                             with zero or more elements,
   *                            'false' when array generation failed for
   *                            some reason.
   */
  bool GetFileList(TStringArray &aRegularFileArray, 
                   TStringArray &aDirectoryFileArray, 
                   const std::string &aRegExp = "");


  /** Close the directory.
   *
   * This function closes a previously opened directory.
   * @return                    'true' when directory was succesfully closed,
   *                            'false' when directory closing failed.
   */
  bool Close(void);

  
  /** Tests whether a given file is regular or not.
   * 
   * @param[in] aFilename       Full path to filename to test
   * @return                    'true' if file is regular (and exists),
   *                            'false' if not regular or doesn't exist.
   */
  static bool IsRegular(const std::string &aFilename);


  /** Return the basename part of the given path.
   *
   * This is a wrapper for libc 'basename' function.
   * See 'basename' function description from the libc documentation
   * for details.
   *
   * @param[in] aPath           Path to split
   * @return                    Basename part of the path
   */
  static std::string GetBasename(const std::string &aPath);


  /** Return the directory part of the given path.
   *
   * This is a wrapper for libc 'dirname' function.
   * See 'dirname' function description from the libc documentation
   * for details.
   *
   * @param[in] aPath           Path to split
   * @return                    Directory part of the path
   */
  static std::string GetDirname(const std::string &aPath);

private:
  CDirectory(const CDirectory &) 
    : iDirectoryHandle(),
      iDirectoryPath() {}
  CDirectory &operator=(const CDirectory &) { return *this; }
  
  DIR *iDirectoryHandle;
  std::string iDirectoryPath;

};

#endif
